<?php

namespace App\Hospital\Controllers;

use App\Hospital\Repositories\HospitalUserBreak;
use App\Models\Family;
use App\Models\Hospital;
use App\Models\HospitalUserBreak as ModelsHospitalUserBreak;
use App\Models\HospitalUserFamily;
use App\Models\ImportFamily;
use App\Models\SystemNotice;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class HospitalUserBreakController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new HospitalUserBreak(['family','user']), function (Grid $grid) {
            $grid->model()->where('hospital_id',Admin::user()->hospital_id);
            $grid->column('family.name','姓名');
            $grid->column('user.phone','电话');
            $grid->column('status')->using(ModelsHospitalUserBreak::$statusTxt);
            $grid->column('content');
            $grid->column('opinion');
            $grid->column('audit_time');
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('status')->select(ModelsHospitalUserBreak::$statusTxt);;

            });
            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableViewButton();
            $grid->disableQuickEditButton();
            $grid->disableRowSelector();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new HospitalUserBreak(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('hospital_id');
            $show->field('family_id');
            $show->field('status');
            $show->field('content');
            $show->field('image');
            $show->field('opinion');
            $show->field('audit_time');
            $show->field('reviewer');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new HospitalUserBreak(['family','user']), function (Form $form) {
            $form->display('user.phone','签约用户手机号');
            $form->display('family.name','签约人姓名');
            $form->display('content');
            $form->display('image');
            $form->textarea('opinion');
            $form->select('status')->options(ModelsHospitalUserBreak::$statusTxt);

            $form->display('created_at','解约提交时间');

            $form->hidden('hospital_id');
            $form->hidden('family_id');
            $form->hidden('user_id');

            $form->saving(function (Form $form){
                $form->audit_time = date('Y-m-d H:i:s');
                $family = Family::query()->where('id',$form->family_id)->first();
                $hospital = Hospital::query()->where('id',$form->hospital_id)->first();
                if($form->status == 1){
                    $hospitalUserFamily = HospitalUserFamily::query()
                        ->where('status',1)
                        ->where('hospital_id',$form->hospital_id)
                        ->where('family_id',$form->family_id)
                        ->first();
                    if($hospitalUserFamily){
                        try {
                            $hospitalUserFamily->status = 0;
                            $hospitalUserFamily->endtime = date('Y-m-d');
                            $hospitalUserFamily->save();
                            $systemNotice = new SystemNotice();
                            $systemNotice->title = "解约成功";
                            $systemNotice->content = "尊敬的用户您好，您的申请：".$family->name."已与".$hospital->name."解约成功";
                            $systemNotice->type_id = 3;
                            $systemNotice->user_id = $form->user_id;
                            $systemNotice->save();

                        }catch (\Exception $e) {
                            throw new \Exception($e->getMessage());
                        }
                    }else{
                        return $form->response()->error('签约关系不存在');
                    }
                }elseif($form->status == 2){
                    $systemNotice = new SystemNotice();
                    $systemNotice->title = "解约失败";
                    $systemNotice->content = "尊敬的用户您好，您申请的".$family->name."已与".$hospital->name."解约失败。原因：".$form->opinion;
                    $systemNotice->type_id = 4;
                    $systemNotice->user_id = $form->user_id;
                    $systemNotice->save();
                }
            });
        });
    }
}
